package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.domain.dto.SkuStockDTO;
import com.spzx.product.mapper.SkuStockMapper;
import com.spzx.product.service.SkuStockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "sku库存模块")
@RestController
@RequestMapping("stock")
public class SkuStockController {
    @Resource
    SkuStockMapper skuStockMapper;
    @Resource
    SkuStockService skuStockService;
    @InnerAuth
    @Operation(summary = "内部调用接口：提交订单验库存锁库存")
    @PostMapping("checkAndLockSkuStock")
    public R<List<SkuStockDTO>> checkAndLockSkuStock(@RequestBody List<SkuStockDTO> skuStockDTOS){

        return R.ok(skuStockService.checkAndLockSkuStock(skuStockDTOS));
    }

    @InnerAuth
    @Operation(summary = "内部调用接口：根据skuId查询库存数据")
    @GetMapping("getSkuStock/{skuId}")
    public R<SkuStock> getSkuStock(@PathVariable("skuId")Long skuId){
        //我们项目  一个sku只有一条库存记录
        // 下面生成的sql： select * from sku_stock
        // where sku_id = #{skuId} and del_flag = 0  limit 0 , 1;
        SkuStock skuStock = skuStockMapper.selectOne(Wrappers.lambdaQuery(SkuStock.class)
                .eq(SkuStock::getSkuId, skuId)
                .last("limit 1"));
        return R.ok(skuStock);
    }
}
